package com.google.code.swingonfire.builder;

import java.util.Collection;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.ListCellRenderer;

public class JComboBoxBuilder extends AbstractJComponentBuilder<JComboBoxBuilder, JComboBox>
{

    private Object[] items;
    private Object selected;
    private ListCellRenderer renderer;

    public JComboBoxBuilder()
    {
        super(JComboBox.class);
    }

    public JComboBoxBuilder items(Object[] items)
    {
        this.items = items;

        return this;
    }

    public JComboBoxBuilder items(Collection<?> items)
    {
        if (items != null)
        {
            this.items = items.toArray(new Object[items.size()]);
        }

        return this;
    }

    public JComboBoxBuilder selected(Object selected)
    {
        this.selected = selected;

        return this;
    }

    public JComboBoxBuilder renderer(ListCellRenderer renderer)
    {
        this.renderer = renderer;

        return this;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void populate(JComboBox component)
    {
        super.populate(component);

        if (items != null)
        {
            component.setModel(new DefaultComboBoxModel(items));
        }

        if (selected != null)
        {
            component.setSelectedItem(selected);
        }

        if (renderer != null)
        {
            component.setRenderer(renderer);
        }
    }
}
